package com.spade.dsms.service.serviceimpl;

import com.spade.dsms.mapper.UserInfoMapper;
import com.spade.dsms.mapper.UserPasswordMapper;
import com.spade.dsms.pojo.UserInfo;
import com.spade.dsms.pojo.UserPassword;
import com.spade.dsms.pojo.UserPasswordExample;
import com.spade.dsms.service.UserService;
import com.spade.dsms.service.model.UserModel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @author Weikang  Wang
 * @program: com.spade.dsms.service.serviceImpl
 * @description:
 * @date 2019/11/5 10:35
 */
@Service
public class UserServiceImpl implements UserService {

    public static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserPasswordMapper passwordMapper;


    /**
     * 获取用户信息
     *
     * @param id id
     * @return 用户信息
     */
    @Override
    public UserModel getUserInfo(Integer id) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);
        UserPasswordExample example = new UserPasswordExample();
        example.or().andUserIdEqualTo(id);
        List<UserPassword> passwordList = passwordMapper.selectByExample(example);
        if (null == userInfo || null == passwordList.get(0)) {
            return null;
        }

        return convertUser(userInfo, passwordList.get(0));
    }

    /**
     * 用户 注册
     *
     * @param userModel 用户model
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(UserModel userModel) {
        if (null == userModel) {
            logger.error("userModel为空，参数异常！");
        }
        if (StringUtils.isEmpty(userModel.getUserName())
                || null == userModel.getGender()
                || null == userModel.getAge()
                || StringUtils.isEmpty(userModel.getPhone())) {
            logger.error("参数不合法");
        }
        // 插入用户信息
        userInfoMapper.insertSelective(convertToUserInfo(userModel));
        // 插入用户密码
        passwordMapper.insertSelective(convertToUserPassward(userModel));
    }


    /**
     * 转换类
     *
     * @param info     用户信息
     * @param password 密码
     * @return userModel
     */
    private UserModel convertUser(UserInfo info, UserPassword password) {
        UserModel userModel = new UserModel();

        userModel.setId(info.getId());
        userModel.setUserName(info.getUserName());
        userModel.setGender(info.getGender());
        userModel.setAge(info.getAge());
        userModel.setPhone(info.getPhone());
        userModel.setRegisterMode(info.getRegisterMode());
        userModel.setThirdPartyId(info.getThirdPartyId());
        userModel.setPassword(password.getPassword());

        return userModel;
    }

    /**
     * 转换
     *
     * @param userModel 用户注册传参
     * @return userInfo
     */
    private UserInfo convertToUserInfo(UserModel userModel) {
        if (null == userModel) {
            return null;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userModel.getUserName());
        userInfo.setAge(userModel.getAge());
        userInfo.setGender(userModel.getGender());
        userInfo.setPhone(userModel.getPhone());

        return userInfo;
    }

    /**
     * 转换
     *
     * @param userModel 用户传参
     * @return 密码
     */
    private UserPassword convertToUserPassward(UserModel userModel) {
        if (null == userModel) {
            return null;
        }

        UserPassword userPassword = new UserPassword();
        userPassword.setUserId(userModel.getId());
        userPassword.setPassword(userModel.getPassword());

        return userPassword;
    }
}
